package com.example.tourmemory.data.record;

import android.content.Context;

import com.example.tourmemory.data.getdata.RecordDataBank;

import java.util.ArrayList;

public class RecordManager{
    // 记录变化监听器
    private OnRecordChangedListener onRecordChangedListener = null;
    // 数据储存
    private final ArrayList<RecordData> recordDataList;

    // 回调接口，记录变化时调用
    public interface OnRecordChangedListener{
        void onManagerChanged();                 // 清除、加载、重设时调用
        void onAddRecord(int changedIndex);     // 增加时调用
        void onRemoveRecord(int changedIndex, RecordData changedRecord);  // 删除时调用
    }

    public RecordManager(Context context){
        recordDataList = RecordDataBank.LoadRecords(context);
    }

    public RecordManager(ArrayList<RecordData> recordDataList){
        this.recordDataList = recordDataList;
    }

    public void setOnRecordChangedListener(OnRecordChangedListener onRecordChangedListener) {
        this.onRecordChangedListener = onRecordChangedListener;
    }

    public RecordData get(int index){
        return recordDataList.get(index);
    }

    // 添加方法
    public boolean add(RecordData recordData){
        recordDataList.add(recordData);
        if(onRecordChangedListener != null)
            onRecordChangedListener.onAddRecord(size() - 1);
        return false;
    }

    // 删除方法
    public void remove(int index){
        RecordData recordData = recordDataList.remove(index);
        if(onRecordChangedListener!= null)
            onRecordChangedListener.onRemoveRecord(index, recordData);
    }

    // 清除方法
    public void clear(){
        recordDataList.clear();
        if(onRecordChangedListener!= null)
            onRecordChangedListener.onManagerChanged();
    }

    // 重设方法
    public boolean reSet(ArrayList<RecordData> recordDataList){
        clear();
        this.recordDataList.addAll(recordDataList);
        if(onRecordChangedListener!= null)
            onRecordChangedListener.onManagerChanged();
        return false;
    }

    // 大小
    public int size(){
        return recordDataList.size();
    }

    // 是否为空
    public boolean isEmpty(){
        return recordDataList.isEmpty();
    }

    // 保存单个记录到历史数据
    public void saveRecordToBank(Context context, RecordData recordData){
        RecordDataBank.saveRecord(context, recordData);
    }

    // 从历史数据删除单个记录
    public void deleteRecordFromBank(Context context, RecordData recordData){
        RecordDataBank.deleteRecord(context, recordData);
    }

    // 从历史数据加载
    public void loadFromBank(Context context){
        recordDataList.clear();
        recordDataList.addAll(RecordDataBank.LoadRecords(context));
        if(onRecordChangedListener!= null)
            onRecordChangedListener.onManagerChanged();
    }

    // 保存所有记录到历史数据
    public void saveAllToBank(Context context){
        RecordDataBank.SaveRecords(context, recordDataList);
    }

    public RecordManager getCopy(){
        ArrayList<RecordData> recordDataList = new ArrayList<>(this.recordDataList);
        return new RecordManager(recordDataList);
    }

    // 根据日期搜索
    public RecordManager searchByDate(Context context, String date){
        RecordManager recordManager = new RecordManager(new ArrayList<>());
        for(int i = 0; i < recordDataList.size(); i++){
            if(recordDataList.get(i).getDate(context).contains(date)){
                recordManager.add(recordDataList.get(i));
            }
        }
        return recordManager;
    }

    // 根据地点搜索
    public RecordManager searchByAddress(String address){
        RecordManager recordManager = new RecordManager(new ArrayList<>());
        for(int i = 0; i < recordDataList.size(); i++){
            if(recordDataList.get(i).getAddress().contains(address)){
                recordManager.add(recordDataList.get(i));
            }
        }
        return recordManager;
    }

    // 根据标签搜索
    public RecordManager searchByTag(String tag){
        RecordManager recordManager = new RecordManager(new ArrayList<>());
        for(int i = 0; i < recordDataList.size(); i++){
            if(recordDataList.get(i).getTag().contains(tag)){
                recordManager.add(recordDataList.get(i));
            }
        }
        return recordManager;
    }

    // 根据描述、地点、标签搜索
    public RecordManager search(String key){
        RecordManager recordManager = new RecordManager(new ArrayList<>());
        for(int i = 0; i < recordDataList.size(); i++){
            if(recordDataList.get(i).getDescription().contains(key)
                    || recordDataList.get(i).getAddress().contains(key)
                    || recordDataList.get(i).getTag().contains(key)){
                recordManager.add(recordDataList.get(i));
            }
        }
        return recordManager;
    }

    public void remove(RecordData recordData){
        for(int i = 0; i < recordDataList.size(); i++){
            if(recordDataList.get(i).equals(recordData)){
                remove(i);
                return;
            }
        }
    }
}
